import 'package:common/common.dart';
import 'package:common/net/api_status.dart';

/// Base页面
abstract class BaseWidget extends ConsumerWidget {
  const BaseWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return buildContent(context, ref);
  }

  Widget buildContent(BuildContext context, WidgetRef ref);
}

/// Base页面状态
abstract class BaseState<T extends ConsumerStatefulWidget, M extends BaseWidgetModel, S extends BasePageState> extends ConsumerState<T> {
  late M widgetModel;
  late S pageState;

  @override
  Widget build(BuildContext context) {
    DataState dataState = ref.watch(pageState.dataStateProvider.notifier).state;
    switch (dataState) {
      // 失败状态
      case DataState.error:
        var errorCode = ref.read(pageState.errorMsgProvider.notifier).state;
        return buildError(context, errorCode);
      // 请求后空数据状态
      case DataState.noData:
        return buildNoData(context);
      // 初始状态
      case DataState.idle:
      // 初始化
      case DataState.init:
      // 刷新状态
      case DataState.refreshing:
      // 加载状态
      case DataState.loading:
      // 成功状态
      case DataState.success:
      default:
        break;
    }
    return buildView(context, dataState);
  }

  Widget buildView(BuildContext context, DataState dataState);

  Widget buildError(BuildContext context, int errorCode);

  Widget buildNoData(BuildContext context);

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    widgetModel.didChangeDependencies();
  }

  @override
  void didUpdateWidget(covariant T oldWidget) {
    super.didUpdateWidget(oldWidget);
    widgetModel.didUpdateWidget(oldWidget);
  }

  @override
  void reassemble() {
    super.reassemble();
    widgetModel.reassemble();
  }

  @override
  void deactivate() {
    super.deactivate();
    widgetModel.deactivate();
  }

  @override
  void activate() {
    super.activate();
    widgetModel.activate();
  }

  @override
  void dispose() {
    super.dispose();
    widgetModel.dispose();
  }
}

/// Base业务处理
mixin class BaseWidgetModel<T extends ConsumerStatefulWidget> {
  void didChangeDependencies() {}

  void didUpdateWidget(T widget) {}

  void reassemble() {}

  void deactivate() {}

  void activate() {}

  void dispose() {}
}

/// Base页面状态提供器
mixin class BasePageState {
  final dataStateProvider = StateProvider<DataState>((ref) => DataState.idle);
  final errorMsgProvider = StateProvider<int>((ref) => 0);
}

class SimplePage extends BaseWidget {
  const SimplePage({Key? key}) : super(key: key);

  @override
  Widget buildContent(BuildContext context, WidgetRef ref) {
    final pageState = SimplePageState();
    final widgetModel = SimpleWidgetModel(ref, pageState);
    return Scaffold(
        appBar: AppBar(
            elevation: 0.0,
            centerTitle: true,
            title: GestureDetector(child: Consumer(builder: (context, ref, child) {
              var title = ref.watch(pageState.titleProvider.notifier);
              return Text(title.state);
            }), onTap: () {
              widgetModel.updateTitle();
            })));
  }
}

class SimpleStatePage extends ConsumerStatefulWidget {
  const SimpleStatePage({Key? key}) : super(key: key);

  @override
  ConsumerState<ConsumerStatefulWidget> createState() => SimpleState();
}

class SimpleState extends BaseState<SimpleStatePage, SimpleWidgetModel, SimplePageState> {
  @override
  void initState() {
    super.initState();
    pageState = SimplePageState();
    widgetModel = SimpleWidgetModel<SimpleStatePage>(ref, pageState);
  }

  @override
  Widget buildView(BuildContext context, DataState state) {
    return Scaffold(
        appBar: AppBar(
            title: GestureDetector(child: Consumer(builder: (context, ref, child) {
      var title = ref.watch(pageState.titleProvider.notifier);
      return Text(title.state);
    }), onTap: () {
      widgetModel.updateTitle();
    })));
  }

  @override
  Widget buildError(BuildContext context, int errorCode) {
    return buildView(context, DataState.error);
  }

  @override
  Widget buildNoData(BuildContext context) {
    return buildView(context, DataState.noData);
  }
}

class SimplePageState with BasePageState {
  final titleProvider = StateProvider((ref) => "标题");
}

class SimpleWidgetModel<T extends ConsumerStatefulWidget> with BaseWidgetModel<T> {
  final WidgetRef ref;

  final SimplePageState state;

  SimpleWidgetModel(this.ref, this.state);

  void updateTitle() {
    var titleName = ref.read(state.titleProvider.notifier).state;
    if (titleName == "哈哈") {
      titleName = "标题";
    } else {
      titleName = "哈哈";
    }
    ref.refresh(state.titleProvider.notifier).state = titleName;
  }
}
